#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<vector>
using namespace std;
class Solution {
public:
    int rob(vector<int>& nums) {
        int n = nums.size();
        return max(nums[0] + dp1(nums, 2, n - 2), dp1(nums, 1, n - 1));
    }
    int dp1(vector<int>& nums, int left, int right)
    {
        if (left > right)
        {
            return 0;
        }
        vector<int> as(nums.size());
        auto ad = as;
        as[left] = nums[left];
        for (int i = left + 1; i <= right; i++)
        {
            as[i] = ad[i - 1] + nums[i];
            ad[i] = max(as[i - 1], ad[i - 1]);
        }
        return max(as[right], ad[right]);
    }
};